<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use think\facade\Db;
class Address
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
		$res = Db::name("address")->select();
		foreach($res as $key=>$value){
			$arr =Db::query("select name from user where id=?",[$value["userId"]]);
			$res[$key] = $res[$key]+$arr[0];
		}
		return json(["code"=>200,"data"=>$res]);
    }
	public function findAddByUserId(){
		$id = input("get.id");
		$res = Db::name("address")->where("userId",$id)->select();
		return json(["code"=>200,"data"=>$res]);
	}
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
		$p = input("post.");
		$date = date('Y-m-d H:i:s',time());
		$data = ["consignee"=>$p["consignee"],"mobile"=>$p["mobile"],"is_default"=>$p["selected"],"userId"=>$p["userValue"],"createTime"=>$date,"updateTime"=>$date,"province_name"=>$p["province"],"city_name"=>$p["city"],"area_name"=>$p["area"],"addr_detail"=>$p["addr_detail"]];
		Db::table("address")->insert($data);
		return json(["code"=>200,"msg"=>"操作成功"]);
	}

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        //
		$p = input("post.");
		$date = date('Y-m-d H:i:s',time());
		$data = ["id"=>$p["id"],"consignee"=>$p["consignee"],"mobile"=>$p["mobile"],"is_default"=>$p["selected"],"userId"=>$p["userValue"],"createTime"=>$p["createTime"],"updateTime"=>$date,"province_name"=>$p["province"],"city_name"=>$p["city"],"area_name"=>$p["area"],"addr_detail"=>$p["addr_detail"]];
		Db::table("address")->save($data);
		return json(["code"=>200,"msg"=>"操作成功"]);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        //
		$id = input("get.id");
		Db::table("address")->delete($id);
		return json(["code"=>200,"msg"=>"操作成功"]);
    }
}
